//
//  BindViewController.m
//  leo
//
//  Created by 常艳 on 15/10/1.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "BindViewController.h"
#import "AccountViewController.h"
#import "Common.h"
#import "Watch.h"
#import "DataManager.h"
#import "User.h"
#import "LocationViewController.h"
#import "ChatListViewController.h"
#import "GrowthViewController.h"
#import "ManagerViewController.h"
#import "MainPageViewController.h"
@interface BindViewController ()

@end

@implementation BindViewController
-(void)dealloc
{
    [self removeObservers];
}
-(void)viewDidAppear:(BOOL)animated
{
    SyncManager *syncManager=[[SyncManager alloc] init];
    if(syncManager==NULL)
        return;
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;
    NSArray *watchLists=[dataManager getWatchList:user.uid];
    if(watchLists!=NULL&&[watchLists count]>0)
        [self jumnToMainPage];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars=YES;
    self.title=@"添加手表";
    self.navigationItem.leftBarButtonItem.enabled=NO;
    
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"账号" style:UIBarButtonItemStyleDone target:self action:@selector(openAccount)];
    [self.navigationItem setRightBarButtonItem:rightButton];
    rightButton=NULL;
    if(self.type==1)
    {
        self.upLable.text=@"欢迎回来";
        self.downLable.text=@"还没有添加设备";
        self.bindBtn.titleLabel.text=@"扫码绑定";
    }
    else if(self.type==2)
    {
        self.upLable.text=@"绑定请求已发送";
        self.downLable.text=@"等待主管理员确认请求";
        self.bindBtn.titleLabel.text=@"重新扫码";
    }
    [self registerObservers];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)openAccount
{
    AccountViewController *accountViewController=[[AccountViewController alloc]init];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:accountViewController animated:YES];
    self.hidesBottomBarWhenPushed = NO;
}
- (IBAction)scan:(id)sender {
    ScanViewController *scanController=[[ScanViewController alloc] init];
    scanController.type=1;
    [self.navigationController pushViewController:scanController animated:YES];
}
-(void)registerObservers
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:BINDRELATIONCHANGE object:nil];
}
-(void)removeObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BINDRELATIONCHANGE object:nil];
}

- (void)receiveNotification:(NSNotification *)sender {
    if(self.type==2)
    {
        [self jumnToMainPage];
    }
}
-(void)jumnToMainPage
{
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
        return;
    User *user=[User sharedSingleton];
    if(user==NULL)
        return;
    Watch *selectWatch=[dataManager getActiveWatch:user.uid];
    if(selectWatch==NULL)
        return;
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate==NULL)
        return;
    if(selectWatch.role==ADMIN)
    {
        mydelegate.isAdmin=YES;
    }
    else
    {
        mydelegate.isAdmin=NO;
    }
    NSMutableArray *items = [[NSMutableArray alloc]init];
    CGRect rect = [UIScreen mainScreen].bounds;
    mydelegate.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 20+44, rect.size.width, rect.size.height-(20+44))];
    BaseMapViewController *locationViewController = [[LocationViewController alloc]init];
    locationViewController.mapView=mydelegate.mapView;
    mydelegate.search = [[AMapSearchAPI alloc] init];
    [items addObject:locationViewController];
    
    ChatListViewController *chatViewController = [[ChatListViewController alloc]init];
    [items addObject:chatViewController];
    
    
    GrowthViewController *growthViewController = [[GrowthViewController alloc]init];
    [items addObject:growthViewController];
    
    ManagerViewController *managerViewController = [[ManagerViewController alloc]init];
    [items addObject:managerViewController];
    
    
    MainPageViewController *mainPageViewController = [[MainPageViewController alloc]init];
    [mainPageViewController setViewControllers:items];
    
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:mainPageViewController];
    if(mydelegate!=NULL)
        [mydelegate setNavProperty:nav];
    mainPageViewController.extendedLayoutIncludesOpaqueBars = YES;
    managerViewController.motherTabViewController=mainPageViewController;
    locationViewController.motherTabViewController=mainPageViewController;
    growthViewController.motherTabViewController=mainPageViewController;
    self.view.window.rootViewController=nav;
    [self.navigationController popToRootViewControllerAnimated:YES];
    [LeoMsg showMsg:@"您已加入家庭圈"];
}
@end
